<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="律师姓名" prop="lawyerName">
				<el-input v-model="queryParams.lawyerName" placeholder="请输入律师姓名" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="手机号" prop="phone">
				<el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['system:lawyer:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['system:lawyer:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
					@click="handleDelete" v-hasPermi="['system:lawyer:remove']">删除</el-button>
			</el-col>
		<!-- 	<el-col :span="1.5">
				<el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['system:lawyer:export']">导出</el-button>
			</el-col> -->
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="lawyerList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="律师图片" align="center" prop="photoUrl">
				<template slot-scope="scope">
					<el-image style="width: 100px; height: 100px" :src="scope.row.photoUrl" :zoom-rate="1.2"
						:max-scale="7" :min-scale="0.2" :initial-index="4" fit="cover" />
				</template>
			</el-table-column>
			<el-table-column label="律师姓名" align="center" prop="lawyerName" />
			<el-table-column label="教育背景" align="center" prop="eduBackground" />
			<el-table-column label="手机号" align="center" prop="phone" />
			<el-table-column label="律所名称" align="center" prop="firmName" />
			<el-table-column label="律所地址" align="center" prop="firmAddr" />
			<el-table-column label="律师简介" align="center" prop="lawyerResumes" />
			<el-table-column label="是否删除" align="center" prop="isDel">
				<template slot-scope="scope">
					<span>{{ scope.row.isDel == 1?'删除':'未删除' }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['system:lawyer:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['system:lawyer:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改律师对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="律师姓名" prop="lawyerName">
					<el-input v-model="form.lawyerName" placeholder="请输入律师姓名" />
				</el-form-item>
				<el-form-item label="教育背景" prop="eduBackground">
					<el-input v-model="form.eduBackground" placeholder="请输入教育背景" />
				</el-form-item>
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="form.phone" placeholder="请输入手机号" />
				</el-form-item>
				<el-form-item label="律所名称" prop="firmName">
					<el-input v-model="form.firmName" placeholder="请输入律所名称" />
				</el-form-item>
				<el-form-item label="律所地址" prop="firmAddr">
					<el-input v-model="form.firmAddr" placeholder="请输入律所地址" />
				</el-form-item>
				<el-form-item label="律师简介" prop="lawyerResumes">
					<el-input v-model="form.lawyerResumes" type="textarea" placeholder="请输入律师简介"
						:autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
				</el-form-item>
				<el-form-item label="律师图片" prop="photoUrl">
					<!-- <ImageUpload v-model="form.photoUrl" :limit="1" @input="input"></ImageUpload> -->
					<!-- <el-upload ref="field101" :file-list="field101fileList" :action="field101Action"
				:before-upload="field101BeforeUpload">
				<el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
			</el-upload> -->
					<el-upload list-type="picture" action="" accept=".jpg, .png" :limit="1" :auto-upload="false"
						:file-list="fileList" :on-change="changeFile">
						<el-button size="small" type="primary">上传图片</el-button>
						<div slot="tip" class="el-upload__tip">只能上传一张.jpg/.png文件</div>
					</el-upload>
				</el-form-item>
<!--				<el-form-item label="是否删除" prop="isDel">-->
<!--					<el-switch v-model="form.isDel" :active-value="1" :inactive-value="0" />-->
<!--				</el-form-item>-->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listLawyer,
		getLawyer,
		delLawyer,
		addLawyer,
		updateLawyer
	} from "@/api/system/lawyer";
	import {
		upload,
		getUpload
	} from "@/api/system/upload";
	export default {
		name: "Lawyer",
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 律师表格数据
				lawyerList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					lawyerName: null,
					eduBackground: null,
					phone: null,
					firmName: null,
					firmAddr: null,
					lawyerResumes: null,
					isDel: 0
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {
					lawyerName: [{
						required: true,
						message: "律师姓名不能为空",
						trigger: "blur"
					}],
				},
				loading: false,
				photoUrl:'',
				obj:{},
				fileList: [], //深拷贝，判断重名及第一步时的文件信息展示
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询律师列表 */
			getList() {
				var that = this
				that.loading = true;
				that.obj = {}
				that.lawyerList = []
				listLawyer(that.queryParams).then(response => {
					// response.rows.forEach(function(item,index){
					// 	if(item.photoUrl != null){
					// 		that.getLoadImg(item,item.photoUrl)
					// 	}else{
					// 		that.obj.id = item.id
					// 		that.obj.lawyerName = item.lawyerName
					// 		that.obj.eduBackground = item.eduBackground
					// 		that.obj.phone = item.phone

					// 		that.obj.firmName = item.firmName
					// 		that.obj.firmAddr = item.firmAddr
					// 		that.obj.lawyerResumes = item.lawyerResumes
     //          that.obj.isDel = item.isDel
					// 		that.obj.photoUrl = '';
					// 		that.lawyerList.push(that.obj);
					// 	}
					// 	// that.getImgUpload(resp.data.fileId)
					// })
					this.lawyerList = response.rows;
					that.total = response.total;
					that.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					lawyerName: null,
					eduBackground: null,
					phone: null,
					firmName: null,
					firmAddr: null,
					lawyerResumes: null,
					isDel: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				var that = this
				this.queryParams.pageNum = 1;
				this.getList();
			},
			getFile(file) {
				this.getBase64(file.raw).then(res => {
					const formData = new FormData();
					// console.log("2222")
					// formData.append('fileBase64', file);
					axios.post('/system/file/upload', {
						fileBase64: file
					}, {
						headers: {
							'Content-Type': 'multipart/form-data'
						},
						onUploadProgress: e => {
							// 上传进度的处理逻辑
						}
					}).then(res => {
						// 上传成功的处理逻辑
					}).catch(err => {
						// 异常处理逻辑
					})
				});
			},
			// 图片转Base64
			getBase64(file) {
				return new Promise((resolve, reject) => {
					const reader = new FileReader();
					let imgBase64 = '';
					reader.readAsDataURL(file);
					reader.onload = () => {
						imgBase64 = reader.result;
					};
					reader.onerror = error => reject(error);
					reader.onloadend = () => resolve(imgBase64);
				});
			},
			// 文件状态改变时的钩子
			changeFile(file, fileList) {
				var that = this
				// this.form.photoUrl = []; // 表单绑定值
				// console.log("file"+JSON.stringify(file.uid))
				fileList.forEach(async item => {
					const img = await this.getBase64(item.raw);
					upload({
						fileBase64: img
					}).then((resp) => {
						console.log("resp" + resp)
						that.getImgUpload(resp.data.fileId)
						// this.form.photoUrl = resp.url;
					}).catch((e) => {
						this.$message.error(e.message);
						this.$refs.upload.clearFiles();
					})
				});
				// fileList.forEach(async item => {
				//   const img = await this.getBase64(item.raw);
				//   this.form.photoUrl.push(img);
				// });

				// console.log(this.form.photoUrl);
			},
			// 下载图片通过uuid
			getImgUpload(uid) {
				var that = this;
				getUpload(uid).then((resp) => {
					// console.log("resp" + resp)
					that.form.photoUrl = resp.data.fileId;
				}).catch((e) => {
					that.$message.error(e.message);
					that.$refs.upload.clearFiles();
				})
			},
			getLoadImg(item,uid){
				var that = this;
				var obj = {}
				console.log("uid===>>>"+JSON.stringify(uid))

				getUpload(uid).then((resp) => {
					// console.log("111"+resp.data.fileBase64)
					obj.id = item.id
					obj.lawyerName = item.lawyerName
					obj.eduBackground = item.eduBackground
					obj.phone = item.phone
					obj.firmName = item.firmName
					obj.firmAddr = item.firmAddr
					obj.lawyerResumes = item.lawyerResumes
          obj.isDel = item.isDel
					obj.photoUrl = resp.data.fileBase64;
					that.lawyerList.push(obj);
					// return resp.data.fileBase64;
				}).catch((e) => {
					that.$message.error(e.message);
					that.$refs.upload.clearFiles();
				})
			},
			// 处理移除操作
			handleRemove(file, fileList) {
				console.log(file, fileList);
			},
			// 处理预览操作
			handlePreview(file) {
				console.log(file);
			},
			// 处理超出图片个数操作
			handleExceed(files, fileList) {
				this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
			},
			// 移除之前的操作
			beforeRemove(file, fileList) {
				return this.$confirm(`确定移除 ${ file.name }？`);
			},
			// 处理文件上传操作
			handleFileUpload(file) {
				this.loading = true;
				// console.log(file)
				// 调用后端服务器的接口
				var baseFile = this.getBase64(file)
				console.log(baseFile)
				upload(baseFile).then((resp) => {
					this.form.installImgUrl = resp.url;
				}).catch((e) => {
					this.$message.error(e.message);
					this.$refs.upload.clearFiles();
				})
			},
			submitUpload() {
				this.$refs.upload.submit();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.fileList = [];
				this.title = "添加律师";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				console.log(JSON.stringify(row))
				this.reset();
				const id = row.id || this.ids
				getLawyer(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.fileList.push(response.data.photoUrl);
					this.title = "修改律师";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateLawyer(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addLawyer(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除律师"' + row.lawyerName + '"的数据项？').then(function() {
					return delLawyer(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('system/lawyer/export', {
					...this.queryParams
				}, `lawyer_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
